pub mod start_config;

use std::fs::File;
use std::io::Read;
use std::ops::Deref;
use serde::Deserialize;

/// 项目配置
/// 目前确定的文件名是: SelfConfig.toml

static mut self_config:Option<SelfConfig> = None;
///获取SelfConfig单例对象
pub fn get_self_config() -> &'static SelfConfig {
	SelfConfig::get_self_config()
}

#[derive(Deserialize,Debug)]
pub struct SelfConfig{
	pub log     :bool,
	pub path    :Path,
	pub mysql   :MySql,
	pub rocket  :Rocket,
	pub qq_email:QqEmail,
}
impl SelfConfig{
	///单例对象
	pub fn get_self_config() -> &'static Self{
		unsafe {
			match &self_config {
				None => {
					let mut file = File::open("./SelfConfig.toml")
						.expect("[没找到文件] : [./SelfConfig.toml]配置文件");
					let mut s = String::new();
					file.read_to_string(&mut s).unwrap();
					self_config = Some(toml::from_str::<SelfConfig>(&s)
						.expect("[配置文件存在问题] : 来自[./SelfConfig.toml]配置文件"));
					if let Some(v) = &self_config {
						v
					}else {
						todo!("我永远不会被执行")
					}
				}
				Some(v) => v
			}
		}
	}
}
//---------------------------------------------path
#[derive(Deserialize,Debug)]
pub struct Path{
	pub root: String,
	pub cent_piece: String,
	pub tem: String,
}
//---------------------------------------------mysql
#[derive(Deserialize,Debug)]
pub struct MySql{
	pub url: String,
	pub user: String,
	pub password: String,
}
//---------------------------------------------rocket
#[derive(Deserialize,Debug)]
pub struct Rocket {
	pub port: u16,
	pub address: String,
	pub cors : RocketCors
}
impl Default for Rocket{
	fn default() -> Self {
		Rocket {
			port:8000,
			address:"0.0.0.0".to_string(),
			cors : RocketCors {}
		}
	}
}
//------------------------------rocket.cors
#[derive(Deserialize,Debug)]
pub struct RocketCors {

}
//---------------------------------------------qq_email
#[derive(Deserialize,Debug)]
pub struct QqEmail{
	pub name: Option<String>,
	pub account: String,
	pub authorization_code:String
}
//---------------------------------------------